package com.whu.MenuHub.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whu.MenuHub.domain.FoodComment;
import com.whu.MenuHub.domain.FoodCommentDTO;
import com.whu.MenuHub.domain.LabelNum;
import com.whu.MenuHub.domain.User;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.FoodCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/foodcomment")
@CrossOrigin
@Api(tags = "食品评价信息管理API")
public class FoodCommentController {
    @Autowired
    FoodCommentService judgementService;

    @PostMapping("/{userID}")
    @ApiOperation("新增一条食品评价记录")
    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public FoodComment InsertOrder(
            @ApiParam("评价信息")@RequestBody FoodComment foodJudgement,
            @ApiParam("用户ID")@PathVariable Integer userID) throws MenuHubException {
        return judgementService.insertFoodJudgement(foodJudgement);
    }
    @GetMapping ("/label/{userID}")
    @ApiOperation("查询用户的食品购买记录，以标签为分类依据")
    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public List<LabelNum> getlabelNumByUserOrder(@PathVariable Integer userID){
        return judgementService.getlabelNumByUserOrder(userID);
    }
    @PutMapping("/{userID}")
    @ApiOperation("修改一条食品评价记录")
    @PreAuthorize("hasAnyAuthority(#userID)")
    public void updateComment(
            @ApiParam("评价信息")@RequestBody FoodComment foodJudgement,
            @ApiParam("用户ID")@PathVariable Integer userID) throws MenuHubException {
        judgementService.updateJudgementByID(foodJudgement);
    }

    @DeleteMapping("/{id}/{userID}")
    @ApiOperation("删除一条食品评价记录")
    @PreAuthorize("hasAnyAuthority(#userID.toString())")
    public void deleteFoodComment(
            @ApiParam("评价ID")@PathVariable Integer id,
            @ApiParam("用户ID")@PathVariable Integer userID) throws MenuHubException {
        judgementService.deleteJudgementById(id);

    }

    //根据条件查询食品评价信息，条件可以为空
    @ApiOperation("根据条件查询食品评价信息")
    @GetMapping("")
    public IPage<FoodCommentDTO> findUser(@ApiParam("食品ID")@RequestParam(value = "foodID",required = false) Integer foodID,
                                          @ApiParam("订单ID")@RequestParam(value = "orderID",required = false) Integer orderID,
                                          @ApiParam("关键词") @RequestParam(value = "keyword",required = false)  List<String>keyword,
                                          @ApiParam("评价等级")@RequestParam(value = "starLevel",required = false)String starLevel,
                                          @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                          @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition=new HashMap<>();
        if(foodID!=null) {
            condition.put("foodID",foodID);
        }
        if(orderID!=null) {
            condition.put("orderID",orderID);
        }
        //根据评分：好（4-5星）/中（3星）/差（1-2星）评
        if(starLevel!=null) {
            condition.put("starLevel",starLevel);
        }
        if(keyword!=null) {
            condition.put("keyword",keyword);
        }
        return judgementService.getComments(condition, pageNum, pageSize);
    }


}
